﻿using System;
using System.Collections;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Connection;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model
{
    /// <summary>
    /// Operation is used to get counts when super columns are used.
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="SN"></typeparam>
    /// <typeparam name="N"></typeparam>
    public class MultigetSubCountQuery<K, SN, N> : MultigetCountQuery<K, N>
    {
        public MultigetSubCountQuery(Keyspace ks, Serializer<SN> superNameSerializer,
            Serializer<K> keySerializer, Serializer<int> nameSerializer)
            : base(ks, keySerializer, nameSerializer)
        {
            if (superNameSerializer == null)
            {
                throw new NullReferenceException("Super name serializer cannot be null");
            }
            SuperNameSerializer = superNameSerializer;
        }
        public MultigetSubCountQuery<K, SN, N> SetSuperColumn(SN sc)
        {
            SuperColumnName = sc;
            return this;
        }
        protected Serializer<SN> SuperNameSerializer
        {
            get;
            set;
        }
        protected SN SuperColumnName
        {
            get;
            set;
        }
        public new QueryResult<Dictionary<K, int>> Execute()
        {
            if (Keys == null || ColumnFamily == null || SuperColumnName == null)
            {
                throw new NullReferenceException("Keys, Column family and super column name cannot be null");
            }
            MultigetCountOperation<K> operation = new MultigetCountOperation<K>();
            operation.KeySerializer = KeySerializer;
            operation.ColumnFamily = ColumnFamily;
            operation.SlicePredicate = SlicePredicate;
            ExecutionResult<Dictionary<K,int>> execRes = Keyspace.DoExecute(operation);
            QueryResultImpl<Dictionary<K, int>> qryresult = new QueryResultImpl<Dictionary<K, int>>(execRes,null);
            return qryresult;
        }

    }
}
